package com.tan.lbs.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.tan.lbs.dao.DialogNodeDAO;
import com.tan.lbs.entity.DialogNode;
import com.tan.lbs.entity.DialogNodeInTree;
import com.tan.lbs.entity.DialogNodeOrder;
import com.tan.lbs.util.HibernateUtil;

public class DialogNodeDAOImpl implements DialogNodeDAO {

	@Override
	public String create(DialogNode dialogNode) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(dialogNode);
		s.getTransaction().commit();
		return dialogNode.getDialogNodeID();
	}

	@Override
	public DialogNode readByID(String dialogNodeID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		DialogNode dialogNode = (DialogNode)s.get(DialogNode.class, dialogNodeID);
		s.getTransaction().commit();
		return dialogNode;
	}

	@Override
	public void update(DialogNode dialogNode) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.update(dialogNode);
		s.getTransaction().commit();
	}

	@Override
	public void delete(String dialogNodeID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		DialogNode dialogNode = new DialogNode();
		dialogNode.setDialogNodeID(dialogNodeID);
		s.delete(dialogNode);
		s.getTransaction().commit();	
	}

	@Override
	public ArrayList<DialogNode> searchByNPC(String npcID) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		List<DialogNode> gs = s.createCriteria(DialogNode.class).add(Restrictions.
				eq("npcID", npcID)).list();
		s.getTransaction().commit();
		return new ArrayList<DialogNode>(gs);
	}


}
